package com.ruoyi.common.zq.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ruoyi.common.zq.domin.ZqEntryBlank;
import com.ruoyi.common.zq.domin.vo.EntryBlankVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 志愿活动报名表
 *
 * @Author: LoneyFeather
 * @Date: Created in 10:05 2021/12/27
 */
@Mapper
@Repository
public interface EntryBlankMapper extends BaseMapper<ZqEntryBlank> {



    @Update("<script>" +
            " update zq_entry_blank zeb " +
            "set zeb.is_del = 1 " +
            "where zeb.activity_id in " +
                "<foreach collection='activityIds' item='activity_id' open='(' separator=',' close=')'>" +
                    " #{activity_id} " +
                "</foreach>" +
            "</script>")
    int updateIsDelByActivityIds(@Param("activityIds") Long[] activityIds);

    @Update("<script>" +
            "update zq_entry_blank " +
            "set pitch_on = 0 " +
            "where activity_id = #{activityId} " +
            "</script>")
    int updatePitchOnByActivityId(@Param("activityId") Long activityId);

    @Select("<script>" +
            "select if(avg(grade),avg(grade),0) " +
            "from zq_entry_blank " +
            "where grade is not null " +
            "and grade != 0 " +
            "and user_id = #{userId} " +
            "</script>")
    float agvGrade(@Param("userId") Long userId);


    @Select("<script>" +
            "select zeb.id, zeb.activity_id, zeb.user_id,sd.dept_name, zeb.pitch_on, zeb.grade, zeb.user_name, zeb.comment, \n" +
            " zeb.volunteer_time, zeb.is_del, zeb.create_by, zeb.create_time, zeb.update_by, zeb.update_time, zeb.remark \n" +
            "from zq_entry_blank zeb \n" +
            "left join sys_user su on zeb.user_id = su.user_id \n" +
            "left join sys_dept sd on su.dept_id = sd.dept_id \n" +
            "where is_del = 0 " +
            "and activity_id = #{zqEntryBlankDTO.activityId} " +
            "and pitch_on = #{zqEntryBlankDTO.pitchOn} " +
            " <if test=\" zqEntryBlankDTO.userName != null and zqEntryBlankDTO.userName != '' \" >" +
            "and user_name like concat('%', #{zqEntryBlankDTO.userName}, '%')\n" +
            "</if>" +
            "</script>")
    List<EntryBlankVO> listByDTO(@Param("zqEntryBlankDTO") ZqEntryBlank zqEntryBlankDTO);
}
